//===========供应商管理-enterprise_vendor_manage.js


//触发列表刷新事件
function reloadList(fromListId) {
    doReloadList({pageIndex: 1, node: {id: fromListId}});
}

//列表刷新
function doReloadList(evt) {
    var list = h3ui.widgets[evt.node.id];

    var queryId = list.childIds.find(a => a.includes("ModelQueryForm"));

    var filter = h3ui.widgets[queryId].props.value || {};

    filter = JSON.parse(JSON.stringify(filter));

    var pageId = list.childIds.find(a => a.includes("Pagination"));

    var pageIndex = h3ui.widgets[pageId].props.current;
    var pageSize = h3ui.widgets[pageId].props.pageSize;


    list.emit("load", {pageIndex, pageSize, filter});


    list.props.selectedRowKeys = [];
    list.props.selectedRow = [];

}


// 单元格文本渲染
function cellRender(evt) {
    // 获取模型表格id
    let modelTableId = evt.node.parentId;
    // 获取表单id
    let formListId = h3ui.widgets[modelTableId].parentId;
    let {components, col, row, formatText} = evt.data;
    let value = row[col.key];

    var color;

    if (value == "1") {
        color = "#1E9FFF";
    } else {
        color = "#FF5722";
    }


    components[0] = {
        type: 'Tag',
        props: {
            text: formatText,
            color
        }
    };

}

//企业供应商详情
function enterpriseDetail(evt) {
    console.log(evt);

    var row = evt.data.row;
    imageUrlConvent(row.license);
    imageUrlConvent(row.proxy_card);

    var modal = h3ui.widgets.CustomModal1;

    h3ui.models.t_enterprise_vendor_manage.item = row;

    modal.props.visible = true;

}

//图片转换
function imageUrlConvent(iamges) {
    if (iamges && iamges.length > 0) {
        iamges.forEach(b => {
            b.uid = b.id;
            b.status = "done";
            b.url = "/api/file/download?refId=" + b.id;
        })
    }
}

// 打开弹框
function openApprovalModal() {
    var rows = h3ui.widgets.FormList3.props.selectedRow;
    if (rows.length != 1) {
        h3ui.$message.info("请勾选一条数据");
        return;
    }


    h3ui.models.t_enterprise_vendor_manage.item = rows[0];
    h3ui.models.t_enterprise_vendor_manage.item.reason = "";
    h3ui.models.t_enterprise_vendor_manage.item.vendor_status = "1";

    h3ui.widgets.CustomModal2.props.visible = true;
}


//审批事件
function approvalChange(evt) {
    console.log(evt);

    //不同意时需要填写意见
    var display;
    var required;
    var dataSource = h3ui.models.t_enterprise_vendor_manage;
    if (evt.data.value == "2") {
        display = "";
        required = true;
        dataSource.item.reason = "";
    } else {
        display = "none";
        required = false;
    }
    h3ui.widgets.FormModelItem36.props.visible.display = display;

    var p = dataSource.schema.properties.find(a => a.code == "reason");

    p.required = required;

    //初始化必填
    dataSource.item = dataSource.item;

    //此代码为了解决 单选配置静态数据 失效问题
    evt.node.props.options = [{"label": "同意", "value": "1"}, {"label": "不同意", "value": "2"}];
}

//重设
function resetReason() {
    h3ui.models.t_enterprise_vendor_manage.item.reason = "";
}

//审批提交
async function approvalSubmit() {

    var dataSource = h3ui.models.t_enterprise_vendor_manage;

    var status = dataSource.item.vendor_status;

    var submitService;
    // 审批通过
    if (status == "1") {
        console.log("审批通过");
        var isJon = await createVendorJoin();
        console.log("审批通过调用结束");
        if (!isJon) {
            console.log("审批通过失败.....");
            return false;
        }

    } else {
        //审批驳回
        console.log("审批驳回");
        var reason = dataSource.item.reason || "";
        dataSource.item.reason = reason.trim();
    }

    dataSource.updateBO().then(() => {
        reloadList("FormList3");
        h3ui.widgets.CustomModal2.props.visible = false;
    })
    //此代码为了解决  表单内部校验后 单选配置静态数据 失效问题,
    h3ui.widgets.RadioGroup1.props.options = [{"label": "同意", "value": "1"}, {"label": "不同意", "value": "2"}];

}


//企业供应商数据 加入到 供应商入住里 (t_enterprise_vendor_manage ->  t_enterprise_vendor_manage)
async function createVendorJoin() {
    var vendorJoin = h3ui.models.t_vendor_join;
    var enterprise = h3ui.models.t_enterprise_vendor_manage;
    var sysProperty = enterprise.schema.properties.filter(a => a.isSystemProperty).map(a => a.code);
    var vendorCodeList = vendorJoin.schema.properties.filter(a => !sysProperty.includes(a.code)).map(a => a.code);

    vendorJoin.newItem();

    vendorCodeList.forEach(a => {
        vendorJoin.item[a] = enterprise.item[a];
    });

    //组织机构代码
    vendorJoin.item.organization_code = null;
    //组织机构代码图片
    vendorJoin.item.organization_code_picture = null;
    //税号
    vendorJoin.item.duty_paragraph = null;
    //开票地址
    vendorJoin.item.invoice_adress = enterprise.item.invoice_address;
    //税务登记证复印件
    vendorJoin.item.tax_registration_picture = null;
    //组织类型
    vendorJoin.item.architecture_type = "1";
    //加盟类型
    vendorJoin.item.type = "1";
    //加入状态
    vendorJoin.item.join_status = "0";
    //用户名
    vendorJoin.item.username = enterprise.item.user_nam;
    //运费
    vendorJoin.item.freight = null;
    //平台费
    vendorJoin.item.platform_fee = null;
    //小规模
    vendorJoin.item.small = "0";

    return await vendorJoin.createBO().then(res => {
        console.log("then...");
        // debugger;
        return true;
    }).catch(res => {
        // debugger;
        console.log("catch....");
        return false;
    });
}

/**
 * 页签切换刷新列表
 * @param {} params
 */
function tabsChange(evt) {
    var activeKey = evt.data.activeKey;

    var childIs = h3ui.widgets[activeKey].childIds;

    if (childIs.length > 0) {


        //第一次切换使用系统默认的
        if (!h3ui.widgets[activeKey].props.isNotFirstChange) {
            h3ui.widgets[activeKey].props.isNotFirstChange = true;
            return;
        }
        reloadList(childIs[0]);
    }
}